<?php

/**
 * This is the model class for table "matches".
 *
 * The followings are the available columns in table 'matches':
 * @property integer $_id
 * @property string $datetime
 * @property string $tournament
 * @property string $hometeam
 * @property string $awayteam
 * @property integer $homegoals
 * @property integer $awaygoals
 * @property string $stadium
 * @property string $channel
 * @property integer $edited
 */
class Match extends CActiveRecord{
    public $tournament_id;
    public $channel_id;
    public $fullMatch;
    public $playersArray;
    public $players;

    const CURRENT_PERIOD = "2012-2013";
    public static $PERIODS = array(Match::CURRENT_PERIOD=>Match::CURRENT_PERIOD);


    public static $CHANNELS = array("-","NovaSports 1", "NovaSports 2",
	    "NovaSports 3", "NovaSports 4", "NovaSports 5", "NovaSports 6",
	    "NovaSports 7", "ET 1", "NET", "ET 3", "ERT HD", "ANT1", "MEGA",
	    "ALPHA", "SKAI", "STAR", "ALTER", "Connx TV","ConnxSports 1"
	    ,"ConnxSports 2","ConnxSports 3","ConnxSports 4" );

	    public static  $TOURNAMENTS = array("Πρωτάθλημα", "Κύπελλο",
	    "Play offs", "Europa League", "Champions League",
	    "Φιλικό", "League Cup", "Super Cup", "Μεσογειακό Κύπελλο",
	    "Βαλκανικό Κύπελλο", "Κύπελλο UEFA", "Κύπελλο Κυπελλούχων",
	    "Κυπελλο Εκθέσεων", "Κύπελλο Πρωταθλητριών" );

	    public static $PLAYERS = array(
    "2011-2012"=>array(
       "0 "=>"Αραμπατζής", "1 "=>"Κωνσταντόπουλος", "2 "=>"Μοσχονάς", "3 "=>"Γεωργέας", 
       "4 "=>"Καράμπελας", "5 "=>"Κοντοές", "6 "=>"Δέλλας", "7 "=>"Μανωλάς", 
       "8 "=>"Χέλγκασον", "9 "=>"Καλά", "10"=>"Μπουγαΐδης", 
       "11"=>"Εγγλέζου", "12"=>"Τσουκαλάς", "13"=>"Καφές", 
       "14"=>"Μάκος", "15"=>"Λαγός", "16"=>"Γκερέιρο", "17"=>"Λεονάρντο", 
       "18"=>"Γκέντζογλου", "19"=>"Κατσέτης", "20"=>"Αγκριμπά", 
       "21"=>"Κλωναρίδης", "22"=>"Κάρλος", "23"=>"Γκούντιονσεν", 
       "24"=>"Λυμπερόπουλος", "25"=>"Μπέλεκ", "26"=>"Σιαλμάς", "27"=>"Μπερνς", 
       "28"=>"Τσίτας", "29"=>"Νικολτσής", "30"=>"Βάργκας","31"=>"Αργυρίου","32"=>"Ρίκκα",
	     "33"=>"Φούντας"
       ),"2012-2013"=>array(
       "0 "=>"Αραμπατζής", "1 "=>"Κωνσταντόπουλος", "2 "=>"Μοσχονάς", "3 "=>"Κοντοές", 
       "4 "=>"Μπουγαΐδης", "5 "=>"Ντούμπερετ", "6 "=>"Ματεντζίδης", "7 "=>"Βλάχος", 
       "8 "=>"Τσουκαλάς", "9 "=>"Γιάγκο", "10"=>"Κουτρουμπής", 
       "11"=>"Κορδέρο", "12"=>"Γκερέιρο", "13"=>"Ρίκκα", 
       "14"=>"Λαγός", "15"=>"Φούντας", "16"=>"Αγκιριμπά", "17"=>"Φέτσης", 
       "18"=>"Φροξυλιάς", "19"=>"Αρέστη", "20"=>"Νικολιάς", 
       "21"=>"Βλάχος", "22"=>"Γροντής", "23"=>"Τσίτας", 
       "24"=>"Κατσικοκέρης", "25"=>"Παπαδημητρίου", "26"=>"Φουρτάδο","27"=>"Τσαμουρλίδης",
       "28"=>"Αρκούδας","29"=>"Νικολόπουλος","30"=>"Κατίδης","31"=> "Κοτσαρίδης",
       "32"=>"Κουρέλλας" , "33"=>"Καλογέρης","34"=>"Παυλής","35"=>"Σταματής","36"=>"Τσούπρος")
       );

       /**
        * Returns the static model of the specified AR class.
        * @return Match the static model class
        */
       public static function model($className=__CLASS__)
       {
           return parent::model($className);
       }

       public function init(){
           // $year = date("Y");
           //for($i=$year ;$i<$year+3;$i++){
           //    Match::$PERIODS[$i."-".($i+1)] = $i."-".($i+1);
               // if($this->period == null){
               $this->period = Match::CURRENT_PERIOD;
               //   }
               // }

               }

               /**
                * @return string the associated database table name
                */
               public function tableName()
               {
                   return 'aekdroid';
               }

               /**
                * @return array validation rules for model attributes.
                */
               public function rules()	{
                   // NOTE: you should only define rules for those attributes that
                   // will receive user inputs.
                   return array(
                   array('tournament_id,channel_id,_id, homegoals, awaygoals', 'numerical', 'integerOnly'=>true),
                   array('datetime', 'length', 'max'=>20),
                   array('deleted, period, playersArray','safe'),
                   array('tournament, hometeam, awayteam, stadium, channel', 'length', 'max'=>45),
                   // The following rule is used by search().
                   // Please remove those attributes that should not be searched.
                   array('_id, datetime, tournament, hometeam, awayteam, homegoals, awaygoals, playersArray,
        stadium, channel, period', 'safe', 'on'=>'search'),
                   );
               }

               /**
                * @return array relational rules.
                */
               public function relations()
               {
                   return array(
        'details' => array(self::HAS_MANY, 'Detail', 'match_id','order'=>'min ASC'),
                   );
               }

               public function beforeDelete(){
                   parent::beforeDelete();
                   $this->deleted = 1;
                   $this->save();
                   return false;
               }

               public function beforeSave(){
                   $spl = split(" ",$this->datetime);
                   $t = split("/",$spl[0]);
                   $this->datetime = $t[2] . "-" . $t[1]."-".$t[0]. " " . $spl[1];
                   $this->datetime = strtotime($this->datetime);
                   $this->updated = time();
                   $this->tournament = $this->tournament_id;
                   $this->channel = $this->channel_id;
                   if($this->playersArray != null){
                       $this->players = "#".implode("#,#",$this->playersArray)."#";
                   }
                   return true;
               }

               public function afterFind(){
                   $this->datetime = date('d/m/Y H:i:s',$this->datetime);
                   //$this->updated = date('d/m/Y H:i:s',$this->updated);
                   $this->tournament_id = $this->tournament;
                   if($this->details != null){
                       $this->homegoals = $this->getGoals(Detail::HOME);
                       $this->awaygoals = $this->getGoals(Detail::VISITOR);
                   }
                   $this->channel_id = $this->channel;
                   $this->tournament = Match::$TOURNAMENTS[$this->tournament];
                   $this->channel = Match::$CHANNELS[$this->channel];
                   $this->fullMatch = $this->hometeam."-".$this->awayteam." ".$this->homegoals."-".$this->awaygoals;
                   if(strlen($this->players)>0){
                       $this->playersArray = split("#,#",substr($this->players,1,strlen($this->players)-2));

                   }
               }

               public function getGoals($side){
                   $goals = 0;
                   foreach ($this->details as $detail) {
                       if($detail->side == $side && $detail->type == Detail::GOAL){
                           $goals++;
                       }
                   }
                   return $goals;
               }





               /**
                * @return array customized attribute labels (name=>label)
                */
               public function attributeLabels()
               {
                   return array(
			'_id' => 'ID',
			'datetime' => 'Datetime',
			'tournament' => 'Tournament',
			'hometeam' => 'Hometeam',
			'awayteam' => 'Awayteam',
			'homegoals' => 'Homegoals',
			'awaygoals' => 'Awaygoals',
			'stadium' => 'Stadium',
			'channel' => 'Channel',
			'period' => 'Period',
                   );
               }

               /**
                * Retrieves a list of models based on the current search/filter conditions.
                * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
                */
               public function search()
               {
                   // Warning: Please modify the following code to remove attributes that
                   // should not be searched.

                   $criteria=new CDbCriteria;
                   $criteria->order = '_id DESC';
                   $criteria->compare('_id',$this->_id);
                   $criteria->compare('datetime',$this->datetime,true);
                   $criteria->compare('tournament',$this->tournament,true);
                   $criteria->compare('hometeam',$this->hometeam,true);
                   $criteria->compare('awayteam',$this->awayteam,true);
                   $criteria->compare('homegoals',$this->homegoals);
                   $criteria->compare('awaygoals',$this->awaygoals);
                   $criteria->compare('stadium',$this->stadium,true);
                   $criteria->compare('channel',$this->channel,true);
                   $criteria->compare('period',$this->period);
                   //$criteria->condition = "deleted = 0";

                   return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
                   ));
               }


           }